AC_INIT([GICO-lib], [0.1.0], [a.camara@fis.ucm.es])
AC_CONFIG_AUX_DIR([build-aux])

AM_INIT_AUTOMAKE([foreign -Wall -Werror])

AC_PROG_CC
AC_PROG_RANLIB

# Files to be generated
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile src/Makefile tests/Makefile])

##
# CONFIGURATION FOR THE CHECK LIBRARIES AND HEADERS
##

# Optional path for the CHECK libraries
AC_ARG_WITH([check-path], [AS_HELP_STRING([--with-check-path=prefix],
    [specify where to find the CHECK libraries and include files])],
    [CHECKPATHSET=1], 
    [CHECKPATHSET=0])

    
# extend the path flags for the compiler and the linker
if test $CHECKPATHSET = 1 ; then
    AM_CFLAGS="${AM_CFLAGS} -I${with_check_path}/include"
    AM_LDFLAGS="${AM_LDFLAGS} -L${with_check_path}/lib"
fi

LIBS="$LIBS -lcheck"

##
# TEST FOR THE CHECK PRESENT IN THE SYSTEM
##

# store current CFLAGS and merge with AM_CFLAGS for compilation check   
OLD_CFLAGS=${CFLAGS}
CFLAGS="${AM_CFLAGS} ${CFLAGS}"

# check for the library headers   
AC_MSG_CHECKING([for the CHECK library headers])
# try to compile a file that includes a header of the library CHECK
AC_COMPILE_IFELSE([[#include <check.h>]],
    [AC_MSG_RESULT([found])
        FOUND_CHECK=1;],
    [AC_MSG_RESULT([not found])
        FOUND_CHECK=0;])

# reset original CFLAGS
CFLAGS=$OLD_CFLAGS

# handle results
if test $FOUND_CHECK != 1; then
    AC_MSG_NOTICE([])
    AC_MSG_NOTICE([The CHECK library was not found!])
    if test  $CHECKPATHSET = 1 ; then
        AC_MSG_NOTICE([ The install prefix '$with_check_path' for the CHECK library was set.])
        AC_MSG_NOTICE([ --> Maybe wrong?])
    else
        AC_MSG_NOTICE([ No non-standard install prefix was set.])
        AC_MSG_NOTICE([ --> You might want to use '--with-check-path=PREFIX'])
    fi
    AC_MSG_NOTICE([])
    AC_MSG_ERROR([CHECK library is an essential dependency : cannot build])
fi

##
# CONFIGURATION FOR THE GSL LIBRARIES AND HEADERS
##

# Optional path for the GSL libraries
AC_ARG_WITH([gsl-path], [AS_HELP_STRING([--with-gsl-path=prefix],
    [specify where to find the GSL libraries and include files])],
    [GSLPATHSET=1], 
    [GSLPATHSET=0])

    
# extend the path flags for the compiler and the linker
if test $GSLPATHSET = 1 ; then
    AM_CFLAGS="${AM_CFLAGS} -I${with_gsl_path}/include"
    AM_LDFLAGS="${AM_LDFLAGS} -L${with_gsl_path}/lib"
fi

LIBS="$LIBS -lgsl -lgslcblas"

##
# TEST FOR THE GSL PRESENT IN THE SYSTEM
##

# store current CFLAGS and merge with AM_CFLAGS for compilation check   
OLD_CFLAGS=${CFLAGS}
CFLAGS="${AM_CFLAGS} ${CFLAGS}"

# check for the library headers   
AC_MSG_CHECKING([for the GSL library headers])
# try to compile a file that includes a header of the library GSL
AC_COMPILE_IFELSE([[#include <gsl/gsl_matrix.h>]],
    [AC_MSG_RESULT([found])
        FOUND_GSL=1;],
    [AC_MSG_RESULT([not found])
        FOUND_GSL=0;])

# reset original CFLAGS
CFLAGS=$OLD_CFLAGS

# handle results
if test $FOUND_GSL != 1; then
    AC_MSG_NOTICE([])
    AC_MSG_NOTICE([The GSL library was not found!])
    if test  $GSLPATHSET = 1 ; then
        AC_MSG_NOTICE([ The install prefix '$with_gsl_path' for the GSL library was set.])
        AC_MSG_NOTICE([ --> Maybe wrong?])
    else
        AC_MSG_NOTICE([ No non-standard install prefix was set.])
        AC_MSG_NOTICE([ --> You might want to use '--with-gsl-path=PREFIX'])
    fi
    AC_MSG_NOTICE([])
    AC_MSG_ERROR([GSL library is an essential dependency : cannot build])
fi

# Distribute the changed variables among Makefiles
AC_SUBST([AM_CFLAGS])
AC_SUBST([AM_LDFLAGS])
AC_SUBST([LIBS])

AC_OUTPUT